package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.entity.Menu;
import com.example.demo.entity.RoleMenu;
import com.example.demo.mapper.RoleMenuMapper;
import com.example.demo.mapper.UserRoleMapper;
import com.example.demo.service.MenuService;
import com.example.demo.service.RoleMenuService;
import com.example.demo.service.UserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author moguai
 * @version 1.0
 * @date 2022/3/19 20:49
 */
@Service
public class RoleMenuServiceImpl extends ServiceImpl<RoleMenuMapper, RoleMenu> implements RoleMenuService {
    @Autowired
    MenuService menuService;
    @Autowired
    @Qualifier(value = "userRoleServiceImpl")
    UserRoleService userRoleService;
    @Autowired
    RoleMenuService roleMenuService;
    @Autowired(required = false)
    UserRoleMapper userRoleMapper;
    @Autowired(required = false)
    RoleMenuMapper roleMenuMapper;

    @Override
    public List<Menu> selectMenuByUser(Integer userId) {
        //通过userId查对应的roleId
        List<Integer> roleId = userRoleService.selectRoleIdByUser(userId);
        //再找menu
        if (null == roleId || roleId.size() == 0) {
            return null;
        }
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.in("role_id", roleId);
        List<RoleMenu> list = roleMenuMapper.selectList(wrapper);
        List<Integer> Menu_id = list.stream().distinct().map(RoleMenu::getMenu_id).collect(Collectors.toList());
        return menuService.selectMenus(Menu_id);
    }

    @Override
    public void deleteRoleMenu(String name, List<Integer> ids) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.in(name, ids);
        roleMenuMapper.delete(wrapper);
    }
}
